<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('列入日期')" prop="putDate">
            <DatePicker v-model="form.putDate" :placeholder="$t('列入日期')" @change="removeDateChange" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('列入部门')" prop="putDept">
            <el-input v-model="form.putDept" :placeholder="$t('列入部门')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('列入原因')" prop="putReason">
            <el-input v-model="form.putReason" type="textarea" :placeholder="$t('列入原因')" show-word-limit :maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('移出日期')" prop="removeDate">
            <DatePicker v-model="form.removeDate" :placeholder="$t('移出日期')" @change="removeDateChange" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('移出部门')" prop="removeDept">
            <el-input v-model="form.removeDept" :placeholder="$t('移出部门')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('移出原因')" prop="removeReason">
            <el-input v-model="form.removeReason" type="textarea" :placeholder="$t('移出原因')" show-word-limit :maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-show="!btnHide" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="company:abnormal" />
    </div>
  </div>
</template>

<script>
    import { saveCompanyAbnormal, getCompanyAbnormal } from '@/api/company/abnormal/companyAbnormal'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  putDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  putDept: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  putReason: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                // 是否隐藏保存等按钮
                btnHide: false
            }
        },
        created() {
        },
        mounted() {
          this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getCompanyAbnormal(data.id).then(response => {
                        this.form = response.data
                      this.loading = false
                    })
                } else {
                    this.reset()
                  this.loading = false
                }
                this.form.companyId = this.$route.query.companyId
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                companyId: undefined,
                putDate: undefined,
                putDept: undefined,
                putReason: undefined,
                removeDate: undefined,
                removeDept: undefined,
                removeReason: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        this.form.source = '1'
                        saveCompanyAbnormal(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                              if (this.$route.query.companyId) {
                                this.$store.dispatch('bussRisk/UPDATE_BUSS_RISK_COUNT', { id: this.$route.query.companyId })
                              }
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                                this.loading = false
                            }
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          removeDateChange() {
              if (this.form.putDate && this.form.removeDate && this.form.putDate > this.form.removeDate) {
                this.$message({
                  type: 'warning',
                  message: this.$t('移出日期不能小于列入日期')
                })
                this.$set(this.form, 'removeDate', undefined)
              }
          }

        }

    }
</script>
